package com.xinghanlaser.applet.points;

import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xinghanlaser.applet.base.common.exception.ServiceException;
import com.xinghanlaser.applet.base.flow.core.BasePoint;
import com.xinghanlaser.applet.entity.TopicBind;
import com.xinghanlaser.applet.entity.TopicHandleDetail;
import com.xinghanlaser.applet.service.ITopicBindService;
import com.xinghanlaser.applet.service.ITopicHandleDetailService;
import com.xinghanlaser.applet.service.ITopicHandleHistoryService;
import com.xinghanlaser.applet.service.ITopicService;
import com.xinghanlaser.applet.vo.point.AssignAfterSalesVO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import static com.xinghanlaser.applet.constant.AdminConstant.AFTER_SALES_ROLE_ID;

/**
 * <p>
 *
 * <p>
 *
 * @author: mftuo 陀铭峰
 * @since: 2024/5/14 15:19
 */
@Component
public class AssignAfterSalesPoint extends BasePoint<AssignAfterSalesVO> {
    @Resource
    private ITopicService topicService;

    @Resource
    private ITopicHandleDetailService handleService;

    @Resource
    private ITopicHandleHistoryService historyService;

    @Resource
    private ITopicBindService bindService;

    @Override
    @DSTransactional
    protected void pointOperation(AssignAfterSalesVO vo) {
        final Long topicId = vo.getTopicId();
        final Long userId = vo.getUserId();

        // 校验申请单是否存在
        topicService.validatedTopicId(topicId);

        // 更新处理流程信息
        TopicHandleDetail handleEntity = topicService.getHandleEntityByTopicId(topicId);
        handleEntity.setCurrentPointId(getPointId())
                        .setHandler(vo.getHandler());
        handleService.updateById(handleEntity);

        // 查询是否已经指派过
        TopicBind existBind = bindService.getOne(Wrappers.lambdaQuery(new TopicBind())
                .eq(TopicBind::getRoleId, AFTER_SALES_ROLE_ID)
                .eq(TopicBind::getTopicId, topicId)
                .last("LIMIT 1")
        );

        // 如果是第一次绑定
        if(existBind == null){
            // 保存历史流程信息
            historyService.saveHistory(
                    handleEntity.getId(),
                    getPointId(),
                    "已指派售后[" + vo.getHandler() + "]处理",
                    vo.getHandleNote()
            );

            // 绑定原厂普通售后
            this.bindTopic(topicId, userId);

            return;
        }

        // 判断是否为同一个人
        // 如果是同一个抛出异常
        if (existBind.getUserId().equals(userId)) {
            throw new ServiceException("当前已指派给[" + vo.getHandler() + "]处理");
        }

        // 删除原有绑定关系
        bindService.removeById(existBind.getId());

        // 添加历史流程信息
        historyService.saveHistory(
                handleEntity.getId(),
                getPointId(),
                "已更换售后为[" + vo.getHandler() + "]处理",
                vo.getHandleNote()
        );

        // 绑定原厂普通售后
        this.bindTopic(topicId, userId);

    }

    @Override
    protected Class<AssignAfterSalesVO> getPointDataType() {
        return AssignAfterSalesVO.class;
    }

    @Override
    public Long getPointId() {
        return 11L;
    }

    private void bindTopic(Long topicId,Long userId){
        // 绑定特定售后人员
        TopicBind entity = new TopicBind()
                .setTopicId(topicId)
                .setRoleId(AFTER_SALES_ROLE_ID)
                .setUserId(userId);

        bindService.save(entity);
    }
}
